/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.systemManag.service.impl;

import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.systemManag.bean.InterfacelogQueryBean;
import com.koron.workflow.systemManag.mapper.InterfacelogMapper;
import com.koron.workflow.systemManag.service.InterfacelogService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.*;

@Service
public class InterfacelogServiceImpl implements InterfacelogService {
    private static final Logger logger = LoggerFactory.getLogger(InterfacelogServiceImpl.class);

    @TaskAnnotation("queryForList")
    @Override
    public PageBean queryForList(SessionFactory factory, String appCode, String appName, String inParam,
                                 Integer page, Integer pageSize) {
        InterfacelogMapper mapper = factory.getMapper(InterfacelogMapper.class);
        List<InterfacelogQueryBean> list = mapper.queryForList(appCode, appName, inParam, page, pageSize);
        Integer total = null;
        if (page != null || pageSize != null) {
            total = mapper.countQueryForList(appCode, appName, inParam);
        }
        return new PageBean<>(list, total);
    }

    @TaskAnnotation("queryForObject")
    @Override
    public InterfacelogQueryBean queryForObject(SessionFactory factory, String id) {
        return factory.getMapper(InterfacelogMapper.class).queryForObject(id);
    }

    @TaskAnnotation("deleteTwoMonthData")
    @Override
    public MessageBean deleteTwoMonthData(SessionFactory factory) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            Calendar calendar = Calendar.getInstance();
            Date nowDate = new Date(System.currentTimeMillis());
            calendar.setTime(nowDate);
            // 当前时间前两个月对应时间
            calendar.add(Calendar.MONTH, -2);
            factory.getMapper(InterfacelogMapper.class).deleteTwoMonthData(calendar.getTime());
        } catch (Exception e) {
            factory.close(false);
            logger.error("接口调用日志删除2月前日志失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }
}
